import { Injectable } from '@nestjs/common'
import { Repository } from 'typeorm'
import { InjectRepository } from '@nestjs/typeorm'
import to from 'await-to-js'
import { UserEntity } from 'src/entities/user.entity'
import { FileUploadDto } from './dto/file.dto'
const path = require('path')
const fs = require('fs')
@Injectable()
export class FileService {
  constructor() {}
  async save(file): Promise<[Error, string]> {
    let writeFile
    try {
      writeFile = fs.createWriteStream(
        path.join(__dirname, '../../../', 'upload', `${file.originalname}`),
      )
      writeFile.write(file.buffer)
    } catch (error) {
      return [error, '']
    }

    return [null, process.env.baseURL + file.originalname]
  }
}
